<?php

namespace app\applet\controller;

use app\share\model\Token;
use app\share\model\WeChat;
use think\Validate;

class Cart
{
    public function __construct()
    {
        $this->cart = new \app\applet\model\Cart();
        $this->we_chat = new WeChat();
        $this->token = new Token();
    }

    /**
     * 添加购物车
     */
    public function addCart()
    {
        $input = input();
        $validate = new Validate([
            ['goods_id', 'require', '商品ID不能为空'],
            ['goods_num', 'require', '商品数量不能为空'],
            ['spec_id', 'require', '商品规格ID不能为空'],
            ['source_type', 'require', '商品来源不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['create_time'] = time();
        $input['update_time'] = time();
        $input['member_id'] = get_header_id("token");

        #添加用户购物车数据
        $this->cart->addCart($input);
        returnJson(200,'购物车添加成功');
    }


    /**
     *购物车列表
     * @param string code
     */
    public function cartList()
    {

        $input = input();
        $validate = new Validate([
            ['source_type', 'require', '商品商品来源类型不能为空'],

        ]);
        if(!$validate->check($input ))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");

        $input['member_id']=  $member_id;
        $current_page = empty($input ['current_page']) ? 1 :$input ['current_page'];
        $page_num = empty($input ['page_num']) ? 10: $input ['page_num'];
        $data = $this->cart->getCartList($input,$page_num,$current_page);
        returnJson(200,'success',$data );
    }

    /**
     * 修改购物车 修改商品数量
     */
    public function modifyCartNums(){
        $input=input();
        $validate = new Validate([
            ['cart_id', 'require', '购物车ID不能为空'],
            ['goods_num', 'require', '商品数量不能为空'],
            ['cart_list', 'require', '购物车列表参数不能为空'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }

        $member_id = get_header_id("token");

        $this->cart->updateNums($input,$member_id);

        returnJson(200,'success');
    }

    /**
     * 修改购物车 修改商品配送方式
     */
    public function modifyCartMethod(){
        $input=input();
        $validate = new Validate([
            ['cart_id', 'require', '购物车ID不能为空'],
            ['d_mothod', 'require', '商品数量不能为空'],
            ['cart_list', 'require', '购物车列表参数不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }

        $member_id = get_header_id("token");

        $this->cart->updateDelivery($input,$member_id);

        returnJson(200,'success');
    }

    /**
     * 修改购物车 修改商品配送方式
     */
    public function cartSelected(){
        $input=input();
        $validate = new Validate([
            ['store_id', 'require', '商家ID不能为空'],
            ['cart_list', 'require', '购物车列表参数不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }

        $member_id = get_header_id("token");

        $this->cart->cartSelected($input,$member_id);

        returnJson(200,'success');
    }
    /**
     * 删除购物车
     */
    public function delCart(){
        $param=input();
        $validate = new Validate([
            ['goods_id', 'require', '商品ID不能为空'],
            ['spec_id', 'require', '商品规格ID不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        $this->cart->removeCart($param,$member_id);

        returnJson(200,'success');

    }

    /**
     * 批量删除购物车数据
     */
    public function cartDelAll(){
        $param=input();
        $validate = new Validate([
            ['cart_ids', 'require', '购物车ID不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        $cart_ids=$param['cart_ids'];

        $this->cart->cartDelAll($cart_ids,$member_id);

        returnJson(200,'操作成功');

    }

}